﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using UDMOutlook.Entity;
using Microsoft.Office.Core;

namespace UDMOutlook
{
    public partial class ThisAddIn
    {
        private Dictionary<Outlook.Inspector, InspectorWrapper> inspectorWrappersValue = new Dictionary<Outlook.Inspector, InspectorWrapper>();
        private Outlook.Inspectors inspectors;

        protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new Ribbon1();
        }
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            inspectors = this.Application.Inspectors;
            inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

            foreach (Outlook.Inspector inspector in inspectors)
            {
                Inspectors_NewInspector(inspector);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            inspectors.NewInspector -= new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            inspectors = null;
            inspectorWrappersValue = null;
        }

        void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {
            if (Inspector.CurrentItem is Outlook.MailItem)
            {
                if (!inspectorWrappersValue.ContainsKey(Inspector))
                {
                    inspectorWrappersValue.Add(Inspector, new InspectorWrapper(Inspector));
                }
            }
        }

        #region VSTO generated code

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }

    public class InspectorWrapper : IDisposable
    {
        private Outlook.Inspector inspector;
        //private Menu ribbon;
        private Ribbon1 ribbon;

        public InspectorWrapper(Outlook.Inspector inspector)
        {
            this.inspector = inspector;
            //this.ribbon = new Menu();
            this.ribbon = new Ribbon1();
            //this.inspector.Close += new System.EventHandler(Inspector_Close);
        }

        void Inspector_Close()
        {
            this.Dispose();
        }

        public void Dispose()
        {
            //this.inspector.Close -= new System.EventHandler(Inspector_Close);
            this.inspector = null;
            this.ribbon = null;
        }
    }
}
